using System;
using System.IO;
using System.Diagnostics;

namespace Nairo.Parser.Dom
{
	public class TreePrinterVisitor : IDomVisitor
	{
		int indentLevel = 0;
		string data;
		bool useLowerCaseTags = true;
		
		public bool UseLowerCaseTags {
			get { return useLowerCaseTags; }
		}
		
		public string Data {
			get { return data; }
		}
		
		public TreePrinterVisitor(bool useLowerCaseTags)
		{
			this.data = "";
			this.indentLevel = 0;
			this.useLowerCaseTags = useLowerCaseTags;
		}
		
		public object VisitFileUnit(FileUnit fileUnit, object data)
		{
			foreach (INode node in fileUnit.Children)
				node.AcceptVisitor(this, data);
			return this;
		}
		
		public object VisitHTMLTag(HtmlTag htmlTag, object data)
		{
			this.data += new string('\t', this.indentLevel) + htmlTag.Create(this.useLowerCaseTags) + "\n";
			this.indentLevel++;
			
			foreach (INode node in htmlTag.Children)
				node.AcceptVisitor(this, data);

			if (this.indentLevel > 0)
				this.indentLevel--;
			if (htmlTag.HasChildNodes)
				this.data += new string('\t', this.indentLevel) + htmlTag.CreateEnd(this.useLowerCaseTags) + "\n";
			return this;
		}
		
		public object VisitAspNetCodeBlock(AspNetCodeBlock aspNetCodeBlock, object data)
		{
			this.data += new string('\t', this.indentLevel) + aspNetCodeBlock.InnerText + "\n";
			foreach (INode node in aspNetCodeBlock.Children)
				node.AcceptVisitor(this, data);
			return this;
		}
		
		public object VisitCommentBlock(CommentBlock commentBlock, object data)
		{
			this.data += new string('\t', this.indentLevel) + "<!-- " + commentBlock.Text + " -->\n";
			return this;
		}
		
		public object VisitInnerHTMLBlock(InnerHTMLBlock innerHTMLBlock, object data)
		{
			this.data += new string('\t', this.indentLevel) + innerHTMLBlock.Text;
			return this;
		}
	}
}
